//
//  PeopleMapViewController.m
//  QT
//
//  Created by 甘世清 on 2024/12/28.
//

#import "PeopleMapViewController.h"
#import <MapKit/MapKit.h>
#import "PeopleAnntation.h"
#import "PeopleMKAnnotationView.h"
@interface PeopleMapViewController ()<MKMapViewDelegate>
@property (nonatomic , strong) MKMapView * mapView;
 
@end

@implementation PeopleMapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (void)SQ_UIViewForSelfView{
    [self.view addSubview:self.mapView];
    
//    [self SQ_setMap];
    [self SQ_Header];
}
//-(void)SQ_setMap{
//    CLLocationCoordinate2D coordinate2D = CLLocationCoordinate2DMake([UserModel share].lat, [UserModel share].lng);
//       // 设置地图显示区域的缩放级别
//    MKCoordinateSpan span = MKCoordinateSpanMake(0.045, 0.045);
//       // 初始化一个常量，表示地图对象的显示区域
//       MKCoordinateRegion region = MKCoordinateRegionMake(coordinate2D,span);
//       // 设置地图对象的显示区域
//    [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
//
//  PeopleAnntation *pointAnnotation = [[PeopleAnntation alloc] init];
//    pointAnnotation.coordinate = coordinate2D;
//    pointAnnotation.model = [PictureModel new];
//    [self.jsonArray addObject:pointAnnotation];
//    [self.mapView removeAnnotations:self.mapView.annotations];
//    [self.mapView addAnnotations:self.jsonArray];
//    [self.mapView showAnnotations:self.jsonArray animated:YES];
//
//}
-(void)SQ_Header{
    CLLocationCoordinate2D  location =  [self.mapView convertPoint:CGPointMake(SCREEN_WIDETH/2.f, SCREEN_HEIGHT/2.f) toCoordinateFromView:self.mapView];
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:[NSString stringWithFormat:@"%.7f",location.latitude] forKey:@"lat"];
    [dic setObject:[NSString stringWithFormat:@"%.7f",location.longitude] forKey:@"lng"];
    [dic setObject:@"20" forKey:@"current"];
    [dic setObject:@"1" forKey:@"size"];
    [HttpTool post:@"user/userList" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        
    }];
}
//位置已经改变了
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
    
}
//设置大头针
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    if ([annotation isKindOfClass:[MKUserLocation class]]) {
       
        MKAnnotationView *userLocationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"UserLocationView"];
        PeopleMKAnnotationView *annotationView = (PeopleMKAnnotationView *)[[PeopleMKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"UserLocationView"];
        PictureModel* ann =  [PictureModel new];
        ann.portrait = [UserModel share].portrait;
        annotationView.model = ann;
        return annotationView;
    }
    PeopleAnntation* ann = (PeopleAnntation *)annotation;
    PeopleMKAnnotationView *annotationView = (PeopleMKAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:@"1"];
    if (!annotationView) {
       annotationView = [[PeopleMKAnnotationView alloc] initWithAnnotation:ann  reuseIdentifier:@"1"];
        annotationView.opaque = NO;
    }
    annotationView.model = ann.model;
    return annotationView;
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
   
    
}

/** 懒加载地图视图 */
- (MKMapView *)mapView{
    if (!_mapView) {
        _mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, SCREEN_HEIGHT)];
        /** 设置地图类型 */
        _mapView.mapType = MKMapTypeStandard;
        _mapView.userTrackingMode = MKUserTrackingModeFollow;
        //设置地图可缩放
        _mapView.zoomEnabled = YES;
        //设置地图可滚动
        _mapView.scrollEnabled = YES;
        //设置地图可旋转
        _mapView.rotateEnabled = YES;
        //设置显示用户显示位置
        _mapView.showsUserLocation = YES;
        //为MKMapView设置delegate
        _mapView.delegate = self;
        _mapView.showsBuildings = YES;
        _mapView.showsTraffic = YES;
    }
    return _mapView;
}

@end
